package lgvalle.com.fluxtodo;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;


import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import lgvalle.com.fluxtodo.adapter.TodoRecyclerAdapter;
import lgvalle.com.fluxtodo.flux.actions.ActionsDispatcherDelegate;
import lgvalle.com.fluxtodo.dagger.DaggerComponent;
import lgvalle.com.fluxtodo.flux.fluxbase.Dispatcher;
import lgvalle.com.fluxtodo.flux.stores.TodoStore;

import javax.inject.Inject;

public class TodoActivity extends AppCompatActivity {

    @InjectView(R.id.main_input)
    EditText mainInput;
    @InjectView(R.id.main_layout)
    ViewGroup mainLayout;
    @InjectView(R.id.main_checkbox)
    CheckBox mainCheck;
    @InjectView(R.id.main_list)
    RecyclerView mainList;

    @Inject
    Dispatcher dispatcher;
    @Inject
    ActionsDispatcherDelegate mActionsDispatcherDelegate;
    @Inject
    TodoStore todoStore;
    @Inject
    TodoRecyclerAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        DaggerComponent.create().inject(this);
        setupView();
    }
    // 设置界面
    private void setupView() {
        mainList.setLayoutManager(new LinearLayoutManager(this));
        mainList.setAdapter(listAdapter);
    }

    @OnClick(R.id.main_clear_completed)
    public void onMainClearButtonClick(View view) {
        clearCompleted();
        resetMainCheck();
    }

    @OnClick(R.id.main_checkbox)
    public void onMainCheckBoxClick(View view) {
        checkAll();
    }

    @OnClick(R.id.main_add)
    public void onMainButtonClick(View view) {
        addTodo();
        resetMainInput();
    }

    // 更新ui
    private void updateUI(TodoStore.TodoStoreChangeEvent event) {
        listAdapter.setItems(todoStore.getTodos());
        if (event.needShowDeleteSnake && todoStore.canUndo()) {
            Snackbar snackbar = Snackbar.make(mainLayout, "Element deleted", Snackbar.LENGTH_LONG);
            snackbar.setAction("Undo", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mActionsDispatcherDelegate.undoDestroy();
                }
            });
            snackbar.show();
        }
    }

    // 声明周期的处理
    // onresume时 注册本activity和store 注册到总线
    @Override
    protected void onResume() {
        super.onResume();
        dispatcher.register(this);
        dispatcher.register(todoStore);
    }

    // onpause时取消注册
    @Override
    protected void onPause() {
        super.onPause();
        dispatcher.unregister(this);
        dispatcher.unregister(todoStore);
    }

    // 增加todo
    private void addTodo() {
        if (validateInput()) {
            mActionsDispatcherDelegate.create(getInputText());
        }
    }

    // 切换所有
    private void checkAll() {
        mActionsDispatcherDelegate.toggleCompleteAll();
    }

    // 清除所有已完成
    private void clearCompleted() {
        mActionsDispatcherDelegate.destroyCompleted();
    }

    // 清空输入
    private void resetMainInput() {
        mainInput.setText("");
    }

    // 还原checkbox
    private void resetMainCheck() {
        if (mainCheck.isChecked()) {
            mainCheck.setChecked(false);
        }
    }

    // 验证输入
    private boolean validateInput() {
        return !TextUtils.isEmpty(getInputText());
    }

    // 获取输入数据
    private String getInputText() {
        return mainInput.getText().toString();
    }

    // 收到store event改变时 更新ui

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEventMainThread(TodoStore.TodoStoreChangeEvent event) {
        updateUI(event);
    }
}
